package com.zhiyu.service.impl;

import com.zhiyu.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhiyu.entity.DO.AccountDO;
import com.zhiyu.service.AccountService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AccountServiceImpl implements AccountService {

	@Autowired
	private AccountMapper accountMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public AccountDO addNewAccount(String username, String password) throws Exception {
		try {
			AccountDO accountDO = new AccountDO();
			accountDO.setUsername(username);
			accountDO.setPassword(password);

			accountMapper.insert(accountDO);
			return accountDO;
		} catch(Exception e) {
			throw new Exception();
		}
	}

	@Override
	public boolean checkAccount(String username) {
		AccountDO accountDO = accountMapper.findByUsername(username);

		return accountDO != null;
	}

	@Override
	public AccountDO findById(int id) {
		return accountMapper.findById(id);
	}

	@Override
	public AccountDO findByUsername(String username) {
		return accountMapper.findByUsername(username);
	}


}
